﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pseudogenerator.Common.Generators;

namespace Pseudogenerator.Common.ViewModels
{
    public class FibonacciViewModel
        : Core.WorkSpaceViewModel<Fibonacci>
    {
        public FibonacciViewModel()
        {
            ModeloActual = new Fibonacci();
        }

        #region ModelFields

        public uint m
        {
            get
            {
                return ModeloActual.m;
            }
            set
            {
                if (ModeloActual.m == value)
                    return;
                ModeloActual.m = value;
                OnPropertyChanged();
                RefreshGUI();
            }
        }

        public uint x0
        {
            get
            {
                return ModeloActual.x0;
            }
            set
            {
                if (ModeloActual.x0 == value)
                    return;
                ModeloActual.x0 = value;
                OnPropertyChanged();
                RefreshGUI();
            }
        }

        public uint x_1
        {
            get 
            {
                return ModeloActual.x_1; 
            }
            set 
            {
                if (ModeloActual.x_1 == value) 
                    return;
                ModeloActual.x_1 = value;
                OnPropertyChanged();
            }
        }
        

        #endregion

        public override bool CanRun
        {
            get
            {
                return ModeloActual.CanGenerateNext && !InWorkProgress;
            }
        }

        private Fibonacci _ModeloActual = null;
        public override Fibonacci ModeloActual
        {
            get
            {
                if (_ModeloActual == null)
                    _ModeloActual = new Fibonacci();

                return _ModeloActual;
            }
            set
            {
                if (value == null)
                    return;
                this.x0 = value.x0;
                this.m = value.m;
                this.x_1 = value.Anterior;
                ClearCommand.Execute(null);
            }
        }

        public override string Title
        {
            get { return "Fibonacci"; }
        }

        public override IEnumerable<string> Errors
        {
            get {
                if (m <= 0)
                    yield return "'m' debe ser mayor que 0";
            }
        }

        public override IEnumerable<string> Recommendations
        {
            get { return new List<string>(); }
        }

        protected override void AddPreload()
        {
            var actual = Preloads.FirstOrDefault(i => i.x0 == x0 && i.x_1 == x_1 && i.m == m);
            if (actual == null)
            {
                Preloads.Add(new Fibonacci() { x0 = x0, x_1 = x_1, m = m });
            }
        }
    }
}
